package org.springblade.modules.repair.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.repair.dto.BuildingSubmitDTO;
import org.springblade.modules.repair.entity.Building;
import org.springblade.modules.repair.entity.BusinessApprove;
import org.springblade.modules.repair.enums.ApproveStatusEnum;
import org.springblade.modules.repair.enums.ApproveTypeEnum;
import org.springblade.modules.repair.service.BusinessApproveService;
import org.springblade.modules.repair.service.IBuildingService;
import org.springblade.modules.repair.vo.BuildingSubmitVO;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.repair.mapper.BusinessWorkMapper;
import org.springblade.modules.repair.entity.BusinessWork;
import org.springblade.modules.repair.service.BusinessWorkService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BusinessWorkServiceImpl extends BaseServiceImpl<BusinessWorkMapper, BusinessWork> implements BusinessWorkService{

	@Autowired
	private IBuildingService buildingService;
	@Autowired
	private IUserService userService;
	@Lazy
	@Autowired
	private BusinessApproveService businessApproveService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public HashMap<String,Object> saveBuildingSubmit(BuildingSubmitDTO buildingSubmitDTO) {
		Building building = buildingService.getById(buildingSubmitDTO.getBuildingId());
		BuildingSubmitVO buildingSubmitVO = new BuildingSubmitVO();
		BeanUtil.copyProperties(buildingSubmitDTO,buildingSubmitVO);
		BusinessWork businessWork = new BusinessWork();
		businessWork.setUpdateData(buildingSubmitVO);
		this.save(businessWork);
		BusinessApprove businessApprove = new BusinessApprove();
		businessApprove.setType(ApproveTypeEnum.DATA_ALTER.getId());
		businessApprove.setApproveStatus(ApproveStatusEnum.PENDING.getId());
		businessApprove.setBusinessWorkId(businessWork.getId());
		businessApprove.setSubmitId(buildingSubmitDTO.getSubmitId());
		businessApprove.setWorkOrderId(buildingSubmitDTO.getBuildingId());
		businessApprove.setName(building.getName()+"-"+ApproveTypeEnum.DATA_ALTER.getName());
		User user = userService.getById(buildingSubmitDTO.getSubmitId());
		businessApprove.setPhone(user.getPhone());
		businessApprove.setSubmitName(user.getRealName());
		businessApproveService.save(businessApprove);
		return businessApproveService.getApproveBuildingDetail(businessApprove.getId());
	}
}
